/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client.channel;

import me.l1k3.fx.client.channel.impl.BackgroundPositionExChannelImplJS;
import me.l1k3.fx.client.channel.inter.ChannelContext;
import me.l1k3.fx.client.channel.trait.TraitDecoratorEx;
import me.l1k3.fx.client.engine.inter.FXEngine;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;

public abstract class BackgroundPositionExChannel extends BackgroundPositionChannel {
    protected TraitDecoratorEx originX;

    protected TraitDecoratorEx originY;

    // - - - factory

    public final static BackgroundPositionExChannel createEx(String type) {
        if (type.equals(FXEngine.JS)) {
            return new BackgroundPositionExChannelImplJS();
        }

        return GWT.create(BackgroundPositionExChannel.class);
    }

    public final static BackgroundPositionExChannel rangeEx(String type, int startX, int endX, int startY, int endY, boolean accumulate) {
        BackgroundPositionExChannel channel = createEx(type);
        channel.setXRange(startX, endX);
        channel.setYRange(startY, endY);
        channel.setAccumulate(accumulate);

        return channel;
    }

    public final static BackgroundPositionExChannel byIncrementEx(String type, int diffX, int diffY) {
        BackgroundPositionExChannel channel = createEx(type);
        channel.setXDiff(diffX);
        channel.setYDiff(diffY);
        channel.setAccumulate(true);

        return channel;
    }

    // - - -

    @Override
    protected final void setPosition(Style style, boolean backward) {
        style.setProperty("backgroundPosition", originX.getString(backward) + " " + originY.getString(backward));
    }

    // - - -

    @Override
    public void init(ChannelContext context) {
        Style style = context.getStyle();

        originX = new TraitDecoratorEx(x);
        originY = new TraitDecoratorEx(y);

        if (x.getDiff() == 0 || y.getDiff() == 0) {
            String property = style.getProperty("backgroundPosition");

            if (property.length() > 0) {
                // x y
                String[] positions = property.split(" ");

                if (x.getDiff() == 0) {
                    originX.setDefaultValue(positions[0]);
                }

                if (y.getDiff() == 0) {
                    originY.setDefaultValue(positions[1]);
                }
            }
        }

        setPosition(style, backward);
    }
}
